//
//  ChannelComponent.m
//  ComponentLinkDemo
//
//  Created by zhenby on 2017/3/14.
//  Copyright © 2017年 zhenby. All rights reserved.
//

#import "ChannelComponent.h"
#import "CLPViewComponent.h"

@implementation ChannelComponent

- (BOOL) shouldBeginTransition:(id<CLPTransitionContext>)context {
    return YES;
}

- (void) animateTransition:(id<CLPTransitionContext>)context {

    [self.subComponents enumerateObjectsUsingBlock:^(id<CLPComponent> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj conformsToProtocol:@protocol(CLPViewComponent)]) {
            id<CLPAnimator> animator = [context animatorForLayoutElement:obj];
            if (animator) {
                CGRect iFrame = [context initialFrameForAnimator:animator];
                CGRect fFrame = [context finalFrameForAnimator:animator];
                if (!CGRectEqualToRect(iFrame, fFrame)) {
                    animator.frame = iFrame;
                    [UIView animateWithDuration:1.0 animations:^{
                        animator.frame = fFrame;
                    } completion:^(BOOL finished) {
                        [context completeTransion:finished];
                    }];
                }
            }
        }
    }];
}

@end
